package com.prig.base.controller;

import com.prig.base.DTO.input.PageKeywordSortDto;
import com.prig.base.DTO.input.PermissionDto;
import com.prig.base.entity.Admin;
import com.prig.base.entity.Permission;
import com.prig.base.service.PermissionService;
import com.prig.base.util.response.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

@RestController
@RequestMapping(path = "/permission")
@Api(value = "权限")
public class PermissionController {

    private PermissionService permissionService;

    @Autowired
    public void setpermissionService(PermissionService permissionService) {
        this.permissionService = permissionService;
    }
    @RequestMapping(path = "/getPermissionList",method = RequestMethod.GET)
    @ApiOperation(value = "获取permission列表",notes = "获取permission列表接口")
    @RequiresPermissions("permission:get")
    public Result<List<Permission>> getpermissionList(
            PageKeywordSortDto pageKeywordSortDto){
        Page<Permission> list = permissionService.getPermissions(pageKeywordSortDto);
        return new Result<>(list.getContent(),list.getTotalElements());
    }


    @RequestMapping(path = "/savePermission",method = RequestMethod.POST)
    @ApiOperation(value = "保存permission",notes = "保存permission接口")
    @RequiresPermissions("permission:update")
    public Result savepermission(@RequestBody
                                     @Valid PermissionDto dto){
        System.out.println(dto.getView());
        Permission rqpermission = new Permission();
        BeanCopier copier = BeanCopier.create(dto.getClass(),rqpermission.getClass(),false);
        copier.copy(dto,rqpermission,null);
        if (dto.getId() == 0L){
            Permission permission = permissionService.savePermission(rqpermission);
            if (permission == null){
                return new Result().c400("已存在");
            }
            return new Result();
        }
        rqpermission.setId(dto.getId());
        permissionService.savePermission(rqpermission);
        return new Result();
    }
    @RequestMapping(path = "/delete",method = RequestMethod.POST)
    @ApiOperation(value = "删除permission",notes = "根据permissionId删除permission")
    @RequiresPermissions("permission:delete")
    public Result delete(@NotBlank@ApiParam(value = "权限id",required = true)@RequestBody String ids){
        ids = ids.replaceAll("\"","");
        permissionService.delete(ids);
        return new Result("删除成功");
    }

    @GetMapping(path = "/{id}")
    @ApiOperation(value = "获取permission",notes = "获取permission")
    @RequiresPermissions("permission:get")
    public Result getPermission(@PathVariable(name="id") long id){
        Permission permission = permissionService.getPermission(id);

        if (permission != null){
            List<Permission> all = permissionService.getPermissions();
            all.removeIf(e-> e.getId()==permission.getId());
            permission.children = all;
            return new Result<>(permission);
        }
        return new Result().c400("没有数据");
    }
    @RequestMapping(path = "/getMenu",method = RequestMethod.GET)
    @ApiOperation(value = "获取菜单列表",notes = "获取菜单列表")
    @RequiresAuthentication
    public Result<List<Permission>> getMenu(){
        List<Permission> permissions;
        Subject subject = SecurityUtils.getSubject();
        if (subject.hasRole("sadmin")){
            permissions = permissionService.getMenu(0);
        }else {
            Session session = subject.getSession();
            Admin admin = (Admin)session.getAttribute("user");
            if (admin != null){
                permissions = permissionService.getMenu(admin.getId());
            }else {
                return new Result<>(400,"未登录");
            }
        }
        return new Result<>(permissions);
    }
}
